home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / pi-5ways.zip / PI2.C < prev    next >
C/C++ Source or Header  |  1991-06-15  |  1KB  |  44 lines

  1. /**************************************************************************/
  2. /*       Calculation of π by James Gregory (ca. 1671) approximation       */
  3. /*     π = 4 { 1 - 1/3 + 1/5 - 1/7 + 1/9 ... +[(-1)**n]/[2*k+1] ... }     */
  4. /*                                                                        */
  5. /*                                                                        */
  6. /*                              Mendel Cooper                             */
  7. /*                             3138 Foster Ave.                           */
  8. /*                           Baltimore, MD 21224                          */
  9. /*                                                                        */
  10. /*                                   06/91                                */
  11. /*                   Source code placed in the public domain              */                 
  12. /**************************************************************************/
  13.  
  14.  
  15.  
  16. /* may need #include <math.h> */
  17.  
  18. #define MAX 5000
  19.  
  20. main()
  21. {
  22. double intermediate_result = 0,numerator,Pi;
  23. register int k;
  24.  
  25.  
  26.  
  27.  
  28. for (k = 0; k <= MAX; k++)
  29. {  
  30.       if(k%2)
  31.          numerator = -1.0;
  32.          else numerator = 1.0;
  33.       
  34.       intermediate_result += numerator / (2*(double)k +1);
  35.       Pi = 4.0 * intermediate_result;
  36.  
  37.       printf("Term #%5d ------>  π ≈ %f  \n",k,Pi);  }
  38.  
  39. }
  40.  
  41.  
  42.  
  43.  
  44.